//
//  SEGNewVersionManager.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/3/22.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGNewVersionManager.h"
#import "SEGUpdateAppView.h"

@implementation SEGNewVersionManager

/**
 * 检查版本更新
 * @author Sam
 */
+(void)segCheckVersion{
    
    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:[SEGNewVersionManager urlString ]
                                       parameters:[SEGNewVersionManager inspectPara ]
                                         finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed || ![[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            return;
        }
        SEGVersionModel *data = [SEGVersionModel yy_modelWithJSON:[result valueForKey:@"data"]];
        
        //判断两个版本号是不是相同，展示升级弹窗
        NSString *code = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefaultsClientVersionCodeTypeKey];
        if (![data.versionId isEqualToString:code]) {
            SEGUpdateAppView *updateAppView = [[SEGUpdateAppView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight())];
            updateAppView.contentLabel.text = data.message;
            
            //判断是否强制升级
            if ([data.must isEqualToString:@"1"]) {
                updateAppView.closeButton.hidden = YES;
            }
            
            [updateAppView.updateButton addTarget:self action:@selector(onUpdateAction:) forControlEvents:UIControlEventTouchUpInside];
            [[[UIApplication sharedApplication] keyWindow] addSubview:updateAppView];
            [[NSUserDefaults standardUserDefaults] setObject:data.url forKey:kSEGUserDefaultsAppStoreURL];
        }
    }];
}

/**
 把View放到V层
 @author Sam
 */
+(void)segCheckVersion:(void(^)(SEGVersionModel* data))versionResponse {
    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:[SEGNewVersionManager urlString ]
                                       parameters:[SEGNewVersionManager inspectPara ]
                                         finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed || ![[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            versionResponse(nil);
            return;
        }
        
        SEGVersionModel *data = [SEGVersionModel yy_modelWithJSON:[result valueForKey:@"data"]];
        
        //判断两个版本号是不是相同，展示升级弹窗
        NSString *code = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefaultsClientVersionCodeTypeKey];
        
        if (![data.versionId isEqualToString:code]) {
            [[NSUserDefaults standardUserDefaults] setObject:data.url forKey:kSEGUserDefaultsAppStoreURL];
            versionResponse(data);
            return;
        }
        
        versionResponse(nil);
        return;
    }];
}

+ (void)onUpdateAction:(UIButton*)sender{
    NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:kSEGUserDefaultsAppStoreURL];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

+(void)segSaveVersion{
    NSString *code = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefaultsClientVersionCodeTypeKey];
    [[NSUserDefaults standardUserDefaults] setObject:code forKey:kUserDefaultShowNewVersion];
}
+(NSString*)segGetVersione{
    NSString *versionString = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultShowNewVersion];
    return versionString;
}

// 检查是否有新的版本
+(NSString *)segCompareVersion {
    NSString * isNewVersion = @"1";
    NSString *versionString = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultShowNewVersion];
    if (versionString && [versionString isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:UserDefaultsClientVersionCodeTypeKey]]) {
        isNewVersion = @"0";
    }
    return isNewVersion;
}

// 检查是否有新的版本并且进行跳转
+(void)segCompareVersionAndPush {
    if ([[SEGNewVersionManager segCompareVersion] isEqualToString:@"1"]) { // 融汇不添加引导图
        [[SEGUhomeMediator sharedInstance]seg_pushToNewVersionViewController:nil];
    }else{
        [[SEGUhomeMediator sharedInstance] seg_EnterAppRequestLogic:@{}];
    }
}

#pragma mark - Private Methods
+(NSString*) urlString {
    return [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/bms/version/api/inspect",nil];
}

/**
 新版本
 */
+(NSDictionary*) inspectPara {
    return @{@"clientType":@"1"};
}
@end

